import Foundation

// ComicDetailResponse 现在符合 Decodable 协议
struct ComicDetailResponse: Identifiable, Decodable {
    var id: Int { data.id }  // 使用 data 的 id 作为 ComicDetailResponse 的 id
    var code: Int
    var msg: String?
    var data: ComicDetail  // data 变为单一的 ComicDetail 对象
}

struct ComicEps: Identifiable, Decodable  {
    var id: Int
    var name: String
    var cover: String
}

var comiceps: [ComicEps] = [
    ComicEps(
        id: 1,
        name: "剧集",
        cover: ""
    ),
]

var comicdetail:ComicDetail = ComicDetail(
    id: 1, name: "标题", eps: comiceps,is_fav:1
)

struct ComicDetail: Identifiable, Equatable, Decodable {  // ComicDetail 也要遵循 Decodable 协议
    var id: Int
    var name: String
    var eps: [ComicEps]
    var is_fav:Int
    
 
    
    static func ==(lhs: ComicDetail, rhs: ComicDetail) -> Bool {
        return lhs.id == rhs.id && lhs.name == rhs.name
    }
}

class ComicDetailViewModel: ObservableObject {
    @Published var isLoading = true
    @Published var errorMessage: String?
    @Published var detail: ComicDetail?  // detail 是单一对象
    
    // 获取漫画详情的方法
    func fetchComicDetail(main_id: Int, completion: @escaping (ComicDetail?) -> Void) {
        guard let url = URL(string: "http://www.cosck.com:39999/api/pica/pica_main/get?data_id=\(main_id)") else {
            self.errorMessage = "Invalid URL"
            completion(nil)
            return
        }
        print(url)
        
        var request = URLRequest(url: url)
        request.httpMethod = "GET"  // 设置请求方法
        request.setValue("dev", forHTTPHeaderField: "token")  // 设置 header: token=dev
        
        isLoading = true
        URLSession.shared.dataTask(with: request) { data, response, error in
            DispatchQueue.main.async {
                self.isLoading = false
                
                if let error = error {
                    self.errorMessage = "Error: \(error.localizedDescription)"
                    completion(nil)
                    return
                }
                
                guard let data = data else {
                    self.errorMessage = "No data received"
                    completion(nil)
                    return
                }
                
                do {
                    // 解码为 ComicDetailResponse 类型
                    let decodedResponse = try JSONDecoder().decode(ComicDetailResponse.self, from: data)
                    completion(decodedResponse.data)  // 返回单一的 ComicDetail 对象
                } catch {
                    self.errorMessage = "Failed to decode JSON: \(error.localizedDescription)"
                    print("Failed to decode JSON: \(error)")
                    completion(nil)
                }
            }
        }.resume()
    }
    
    // 获取漫画详情并更新 ViewModel
    func get_detail(main_id: Int) {
        fetchComicDetail(main_id: main_id) { data in
            if let data = data {
                self.detail = data  // 更新 detail
                print("查询详情")
                print(data)
                self.isLoading = false
            }
        }
    }
}
